import { reqRegister } from "../../request/api";
//初始值
const initState = {
  //初始化user
  user: {
    phone: "110",
    nickname: "1",
    password: "123",
  },
};

//actionTypes
const types = {
  //修改user type
  CHANGE_USER: "REGISTER_CHANGE_USER",
};

//reducer 修改数据
const reducer = (state = initState, action) => {
  switch (action.type) {
    // 修改user的reducer
    case types.CHANGE_USER:
      // {type:types.CHANGE_USER,key:"phone",value:""}
      return {
        ...state,
        user: {
          ...state.user,
          [action.key]: action.value,
        },
      };
    default:
      return state;
  }
};
//actionCreators组件派发的动作
export const actions = {
  //修改user的action
  changeUser: (key, value) => ({ type: types.CHANGE_USER, key, value }),
  //注册请求
  reqRegister: (history) => (dispatch, getState) => {
    let { user } = getState().register;
    reqRegister(user).then((res) => {
        if(res.data.code===200){
            //跳转页面
            history.push('/login')
        }
    });
  },
};

//selectors导出数据
export const getUser = state => state.register.user;

export default reducer;
